/*
 * Powered By XY
 * Since 2020
 */

package com.xy.biz.fd.controller;

import java.util.Collection;
import java.util.List;

import com.xy.biz.fd.client.FolderClient;
import com.xy.biz.fd.domain.bo.FolderBO;
import com.xy.biz.fd.domain.req.folder.*;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.SaveChecks;
import com.xy.lang.validation.UpdateChecks;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xy.biz.fd.domain.resp.GetFolderResponse;
import com.xy.biz.fd.domain.query.FolderQuery;
import com.xy.biz.fd.domain.converter.FolderConverter;
import com.xy.biz.fd.service.FolderService;

/**
 *
 *
 * @author ssl
 * @date 2021-1-23
 */
@Api(tags = "文件夹")
@Slf4j
@RestController
@RequestMapping("/api/fd/folder")
public class FolderController implements FolderClient {

    @Autowired
    private FolderService folderService;

    /**
     * 文件夹新增
     * @param request 文件夹名称,知识库id,上级文件夹(可为空，空为一级目录)
     * @return
     */
    @ApiOperation("文件夹新增")
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save")
    @Override
    public Result<GetFolderResponse> saveFolder(@RequestBody SaveFolderRequest request) {
        return folderService.saveFolder(FolderConverter.toBO(request));
    }

    @Deprecated
    @Validatable(groups = {SaveChecks.class})
    @PostMapping(value = "/save_all")
    public Result<?> saveAllFolder(@RequestBody Collection<SaveFolderRequest> collection) {
        folderService.saveAllFolder(FolderConverter.toBO(collection));
        return Result.success();
    }

    /**
     * 文件夹重命名
     * @param request 文件夹id,文件新名称
     * @return
     */
    @ApiOperation("文件夹重命名")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update_name")
    public Result<?> updateNameFolder(@RequestBody SaveFolderRequest request) {
        folderService.updateNameFolder(FolderConverter.toBO(request));
        return Result.success();
    }

    /**
     * 文件夹移动
     * @param request 文件夹id，新父文件夹id
     * @return
     */
    @ApiOperation("文件夹移动")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/update_move")
    public Result<?> updateMoveFolder(@RequestBody SaveFolderRequest request) {
        return folderService.updateMoveFolder(FolderConverter.toBO(request));
    }

    /**
     * 文件夹复制
     * @param request 源文件夹id，新父文件夹id
     * @return
     */
    @ApiOperation("文件夹复制")
    @Validatable(groups = {UpdateChecks.class})
    @PostMapping(value = "/copy_folder")
    public Result<?> copyFolder(@RequestBody SaveFolderRequest request) {
        return folderService.copyFolder(FolderConverter.toBO(request));
    }

    /**
     * 文件夹删除，删除至回收站
     * @param request
     * @return
     */
    @Validatable
    @PostMapping(value = "/delete")
    public Result<?> deleteFolder(@RequestBody GetFolderRequest request) {
        return folderService.deleteFolder(request.getFolderId());
    }


    /**
     * 文件夹批量删除，删除至回收站
     * @param folderIds
     * @return
     */
    @ApiOperation(value = "文件夹批量删除")
    @Validatable
    @PostMapping(value = "/delete_by_ids")
    @Override
    public Result<?> deleteFolderByIds(@RequestBody Collection<Long> folderIds) {
        return folderService.deleteFolderByIds(folderIds);
    }

    @Deprecated
    @Validatable
    @PostMapping(value = "/get")
    @Override
    public Result<GetFolderResponse> getFolder(@RequestBody GetFolderRequest request) {
        return Result.success(FolderConverter.toResponse(folderService.getFolder(request.getFolderId())));
    }

    @Deprecated
    @PostMapping(value = "/list_by_ids")
    @Override
    public Result<List<GetFolderResponse>> listFolderById(@RequestBody Collection<Long> collection) {
        return Result.success(FolderConverter.toResponse(folderService.listFolderById(collection)));
    }

    @PostMapping(value = "/list")
    @Override
    public Result<?> listFolderDocument(@RequestBody ListFolderRequest request) {
        return Result.success(folderService.listFolderDocument(FolderConverter.toQuery(request)));
    }

    @ApiOperation("获取文件夹，文档树状图")
    @PostMapping(value = "/list_tree")
    @Override
    public Result<?> listTreeFolderDocument(@RequestBody ListFolderRequest request) {
        return Result.success(folderService.listTreeFolderDocument(FolderConverter.toQuery(request)));
    }

    @ApiOperation("获取文件夹树状图")
    @PostMapping(value = "/list_tree_folder")
    public Result<?> listTreeFolder(@RequestBody ListFolderRequest request) {
        return Result.success(folderService.listTreeFolder(FolderConverter.toQuery(request)));
    }

    @Deprecated
    @PostMapping(value = "/count")
    @Override
    public Result<Integer> countFolder(@RequestBody ListFolderRequest request) {
        return Result.success(folderService.countFolder(FolderConverter.toQuery(request)));
    }

    @Deprecated
    @PostMapping(value = "/page")
    @Override
    public Result<Page<GetFolderResponse>> pageFolder(@RequestBody ListFolderRequest request) {
        FolderQuery query = FolderConverter.toQuery(request);
        List<GetFolderResponse> list = FolderConverter.toResponse(folderService.listFolder(query));
        int count = folderService.countFolder(query);
        return Result.success(new Page<>(list, count));
    }

    /**
     * 恢复文件，待定
     */
    @ApiOperation("文件夹恢复")
    @PostMapping(value = "/recover")
    @Override
    public Result<?> recoverFolder(@RequestBody GetFolderRequest request) {
        return folderService.recoverFolder(request.getFolderId());
    }

    /**
     * 批量恢复文件夹
     */
    @ApiOperation("文件夹批量恢复")
    @PostMapping(value = "/recover_by_ids")
    @Override
    public Result<?> recoverFolderByIds(@RequestBody Collection<Long> folderIds) {
        return folderService.recoverFolderByIds(folderIds);
    }

    @ApiOperation("获取文件夹信息")
    @PostMapping(value = "/getFolderInfo")
    public Result<?> getFolderInfo(@RequestBody GetFolderRequest request) {
        GetFolderResponse folderInfo = folderService.getFolderInfoById(request.getFolderId());
        return Result.success(folderInfo);
    }

    /**
     * 发行知识库
     * @param
     * @return
     */
    @ApiOperation("发行知识库")
    @PostMapping(value = "/get_release")
    @Override
    public Result<List<GetFolderResponse>> getRelease(@RequestBody ListFolderRequest request) {
        return folderService.getRelease(FolderConverter.toQuery(request));
    }

    @Override
    public Result<GetFolderResponse> createManufactureFolder(CreateManufactureFolderRequest request) {
        return folderService.createManufacture(request);
    }

}
